package com.example.zbh.dsliao.ui.activty;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMContact;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.presenter.BasePresenter;
import com.example.zbh.dsliao.ui.adapter.BlackListAdapter;
import com.example.zbh.dsliao.utils.DliaoUtils;
import com.example.zbh.dsliao.view.CustomDialog;

import java.util.Collections;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 黑名单
 */
public class BlackActivity extends MVPBaseActivity implements View.OnClickListener, BlackListAdapter.OnBlackClickListener {

    private ImageView backSet;//返回按钮
    private RecyclerView recyclerView;
    private BlackListAdapter adapter;
    private GridLayoutManager manager;
    private String username;
    private TextView black_textView;


    @Override
    protected BasePresenter createPresenter() {
        return new BasePresenter();
    }

    @Override
    protected int getLayOut() {
        return R.layout.activity_black;
    }

    @Override
    protected void initView() {
        backSet = (ImageView) findViewById(R.id.backSet);
        backSet.setOnClickListener(this);
        recyclerView = findeView(R.id.blackUserList);
        manager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(manager);
        black_textView = findeView(R.id.black_textView);
    }

    @Override
    protected void initData() {
        EventBus.getDefault().register(this);
        //获取黑名单
        List<String> blackList = EMContactManager.getInstance().getBlackListUsernames();
        //显示黑名单
        if (blackList != null && blackList.size() > 0){
            Collections.sort(blackList);
            adapter = new BlackListAdapter();
            recyclerView.setAdapter(adapter);
            adapter.addData(blackList);
            adapter.setOnBlackClickListener(this);
            black_textView.setVisibility(View.GONE);
        }
        //注册上下文菜单
        registerForContextMenu(recyclerView);

    }

    /**
     * 监听主界面的eventbus发过来的的状态
     * (包括 1 == 账号被服务器删除
     * 2 == 账号被别的设备登录)
     * **/
    public void onEventMainThread(Integer data){
        if (data == 2){
            DliaoUtils.showDialogConnection(this);
        }else if (data == 1){
            DliaoUtils.showDialogUserRemoved(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.backSet:
                this.finish();
                break;
        }
    }

    /**
     * 点击行布局回调username
     * **/
    @Override
    public void onBlackClickListener(View v, final String username) {
        final CustomDialog customDialog = new CustomDialog(this, "正在移出,请稍等..");
        customDialog.show();
       this.username = username;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMContactManager.getInstance().deleteUserFromBlackList(username);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "移出成功..", Toast.LENGTH_SHORT).show();
                            List<String> blackList = EMContactManager.getInstance().getBlackListUsernames();
                            adapter.notifyDataSetChanged();//刷新适配器
                            customDialog.dismiss();
                            if (blackList.size() == 0){
                                black_textView.setVisibility(View.VISIBLE);
                            }
                        }
                    });
                } catch (EaseMobException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "移出失败..", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
